
// 输入的参赛人员姓名格式校验串，之间用英文逗号隔开
var REG_NAME_COMMA = /^([0-9|a-z|A-Z|\u4e00-\u9fa5])+(,[0-9|a-z|A-Z|\u4e00-\u9fa5]+)*$/;

//中文串校验，只校验中文
var REG_NAME_ONLY = /^([\u4e00-\u9fa5])+$/;

//中文串校验，以中文开头
var REG_NAME_START = /^([\u4e00-\u9fa5])+.+$/;

// 登录名校验正则表达式
var REG_LOGINNAME = /^[a-z|A-Z]{1}[0-9|a-z|A-Z|_]+$/;

// 手机号码格式校验串
var REG_MOBILE_PATTERN = /^1[3-9]{1}[0-9]{9}$/;

//座机格式校验串
var REG_PHONE_PATTERN = /^0[5|6][0-9]{2}[-][0-9]{7,8}$/;
//座机格式校验串
var REG_PHONE_PATTERN2 = /^0[5|6][0-9]{10}$/;

// 比例输入值校验
var REG_TATE_PATTERN = /^(0\.[1-9]{1}|1)$/;

var REG_IDCARD_PATTERN = /^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$|^[1-9]{1}[0-9]{16}([0-9]|[xX])[1-9]{2}$/;

function createOptions(id,code,val,type)
{
		/*if(null == id || "" == id)
		{
			return "";
		}*/
		return "<option value='" + code + "' areaId='" + id + "' typeId='" + type + "'>" + val + "</option>";
}

// 根据部门查询下级部门
function showSubArea(obj,renderId)
{
	var options = "<option value='null' selected='selected'>---请选择---</option>";
	// var options = "";
	var val = obj.value;
	if('sd' == val)
	{
		$("#countryDiv").hide();
		return;
	}
	else
	{
		$("#countryDiv").show();
	}
	
	$.ajax({
	       type: "POST",
	       url: ctx+"/gt/area/qrySubArea",
	       dataType: "text",
	       data:"parent.code="+val,
		   async: true,
	       success: function(jsonArea){
	    	   
		       // 转换为json对象
	    	   jsonArea = eval('(' + jsonArea + ')');
		       
			   for(var area in jsonArea)
		   	   {
				 var id = jsonArea[area].id;
		   	  	 var code = jsonArea[area].code;
		   	  	 var name = jsonArea[area].name;
		   	  	 var type = jsonArea[area].type;
		   	  	 options += createOptions(id,code,name,type);
		   	   }
			   $("#" + renderId).html("");
		   	   $("#" + renderId).html(options);
		   	   $("#" + renderId).change();
	       }
	    });
}


//查询在线人数
function qryOnlineCount()
{
	$.ajax({
	       type: "POST",
	       url: ctx+"/gt/online/qryOnlineCount",
	       dataType: "text",
	       data:"",
		   async: true,
	       success: function(onlineCount){
		       // 转换为json对象
	    	   onlineCount = eval('(' + onlineCount + ')');
		       $("#onlineCount").html(onlineCount);
	       }
	    });
}

/**
 * 判空公共方法
 * @param title 提示标题
 * @param objId 判空对象id
 * @param type  判空对象类型
 * @param type  值长度上限
 * @returns {Boolean}
 */
function isNull(title,objId,type,length,bigOrSmall)
{
	if(type == "text")
	{
		var val = $("#"+objId).val();
		if(!$("#"+objId)[0])
		{
			return false;
		}
		if(null == val || "" == val)
		{
			msg(title + ":不能为空。",3000,"warning");
			$("#"+objId).focus();
			return true;
		}else if(length && length > 0)
		{
			// 不能超过length
			if(bigOrSmall && 'small' == bigOrSmall)
			{
				if(val.length > length)
				{
					// alert(title + ":长度不能超过" + length);
					msg(title + ":长度不能超过" + length,3000,"warning");
					$("#"+objId).focus();
					return true;
				}
			}
			else
			{
				if(val.length < length)
				{
					// alert(title + ":长度不能超过" + length);
					msg(title + ":长度不能小于" + length,3000,"warning");
					$("#"+objId).focus();
					return true;
				}
			}
		}
		return false;
	}else
	if(type == "checkBox" || type == "radio")
	{
		var hasChecked = false;
		$('input[name="' + objId + '"]').each(
			function (i,item)
			{
				if(item.checked)
				{
					hasChecked = true;
				}
			}
		);
		if(!hasChecked)
		{
			// alert("请勾选 " + title + "。");
			msg("请勾选 " + title + "。",3000,"warning");
			return true;
		}
		return false;
	}else
	if(type == "select")
	{
		if(!$("#"+objId)[0])
		{
			return false;
		}
		
		var val = $("#"+objId).val();
		if(null == val || "" == val)
		{
			// alert("请选择  " + title + "。");
			msg("请选择  " + title + "。",3000,"warning");
			$("#"+objId).focus();
			return true;
		}
		return false;
	}else
	if(type == "textarea")
	{
		var val = $("#"+objId).val();
		if(null == val || "" == val || "最多输入200字" == val)
		{
			// alert(title + ":不能为空。");
			msg(title + ":不能为空。",3000,"warning");
			$("#"+objId).focus();
			return true;
		}
		return false;
	}else
	if(type == "number")
	{
		var val = $("#"+objId).val();
		if(null != val && "" != val)
		{
			//alert((/^[1-9][0-9]{1}$/g).test(80));
			if(!((/^\d+$/g).test(val) || (/^\d+[.]\d+$/g).test(val)))
			{
				$("#"+objId).focus();
				// alert(title + " 只能输入数字，请重新输入。");
				msg(title + " 只能输入数字或者小数，请重新输入。",3000,"warning");
				return true;
			}
			else
			{
				
				if((/^\d+$/g).test(length))
				{
					if(val.length > length)
					{
						// alert(title + " 只能输入"+length+"位数字。");
						msg(title + " 只能输入"+length+"位数字。",3000,"warning");
						return true;
					}
				}
			}
		}
		else
		{
			msg(title + ":不能为空。",3000,"warning");
			return true;
		}
		return false;
	}else
	// 校验手机号码
	if(type == "mobile")
	{
		if(!$("#"+objId)[0])
		{
			return false;
		}
		
		var val = $("#"+objId).val();
		if(null == val || "" == val)
		{
			msg(title + ":不能为空。",3000,"warning");
			$("#"+objId).focus();
			return true;
		}else
		{
			// 手机号码格式不正确
			if(!((REG_MOBILE_PATTERN).test(val) || (REG_PHONE_PATTERN).test(val)))
			{
				msg(title + ":格式不正确，请重新输入。",3000,"warning");
				$("#"+objId).focus();
				return true;
			}
			
		}
		return false;
	}else
	// 校验身份证号
	if(type == "idcard")
	{
		if(!$("#"+objId)[0])
		{
			return false;
		}
		
		var val = $("#"+objId).val();
		if(null == val || "" == val)
		{
			msg(title + ":不能为空。",3000,"warning");
			$("#"+objId).focus();
			return true;
		}else
		{
			// 手机号码格式不正确
			if(!(REG_IDCARD_PATTERN).test(val))
			{
				msg(title + ":格式不正确，请重新输入。",3000,"warning");
				$("#"+objId).focus();
				return true;
			}
			
		}
		return false;
	}
	
}

// 判断字符串str是否以strEnd结尾
function endWith(str,strEnd)
{
	if(!str || !strEnd || str.length < strEnd.length)
	{
		return false;
	}
	var subEnd = str.substring((str.length - strEnd.length),str.length);
	return subEnd.toLowerCase() == strEnd.toLowerCase();
}


function alertMsg(msg,type)
{
	swal({
	    title: "信息提示：",
	    text: msg,
	    type: 'undefined' == typeof type?"error":type,
	    confirmButtonColor: "#09c",
	    confirmButtonText: "返回",
	    closeOnConfirm: true
	});
}

//add by zad 2018-03-20 type:类型   warning,success,error,info,question  timer:关闭时间，毫秒
function msg(content,timer,type)
{
	if(!type)
	{
		type="success";
	}
	swal({
        title: "",
        text: content,
        type: type,
        showLoaderOnConfirm:true,
        // animation: true,
        // html:html,
        //confirmButtonColor: "#09c",
        //confirmButtonText: "确认",
        //showCancelButton: true,
        //cancelButtonText:"取消",
        // closeOnConfirm: true,
        timer: timer
	}/*, function() {
		window.location.href=ctx + "/logout";
	}*/);
}

//add by zad 2018-03-29 type:类型   warning,success,error,info,question
function comfirmMsg(content,location,backUrl,type,confirmText,cancelText)
{
	/*if(!type)
	{
		type="success";
	}*/
	swal({
        title: "信息提示",
        text: content,
        type: type,
        showLoaderOnConfirm:true,
        // animation: true,
        // html:html,
        //confirmButtonColor: "#09c",
        confirmButtonText: 'undefined' == typeof confirmText?"确认":confirmText,
        showCancelButton: true,
        cancelButtonText: 'undefined' == typeof cancelText?"取消":cancelText,
        closeOnConfirm: true
	}, function(){
		if((typeof location == 'function') || !location)
		{
			location(backUrl);
		}
		else
		{
			window.location.href=location;
		}
		
	});
}

// 公共返回
function goBack(url)
{
	if(url)
	{
		location = url;
	}
	else
	{
		window.history.go(-1);
	}
}

// 公共分页
function page(n,s){
	$("#pageNo").val(n);
	$("#pageSize").val(s);
	document.forms[0].submit();
	return false;
}

// 公共提交
function doSubmit()
{
	document.forms[0].submit();
}


function changeHelpType(obj)
{
	var helpType = $(obj).val();
	var helpO = $("#helpTypeo");
	if(helpO[0].checked)
	{
		$("#helpOther").show();
	}
	else
	{
		$("#helpOther").hide();
		$("#helpOther").val("");
	}
	
}

function openWin(url,title,iWidth,iHeight)
{
	var iTop = (window.screen.availHeight - 20 - iHeight) / 2;
    var iLeft = (window.screen.availWidth - 10 - iWidth) / 2;
    var param = 'height=' + iHeight + ', width=' + iWidth + ', top=' + iTop + ', left=' + iLeft + ', toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no';
    window.open(url, title, param);
}

// 获取cookie值
function getCookie(name)
{
	if(!name)
	{
		return '';
	}
	
	var cooki = document.cookie;
	if(cooki && cooki.indexOf(name + "=") < 0)
	{
		return '';
	}
	var valEnd = cooki.split(name + "=")[1];
	if(!valEnd)
	{
		return '';
	}
	var val = valEnd.split(";")[0];
	return val;
}